/**
 * @title CheckUpgradeCommunicater.java
 * @package com.esun.cheetah.biz.setting
 * @description 检查是否有新版本
 * @author liugan
 * @date 2012-5-22
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.biz.setting;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.esun.cheetah.config.BizCode;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.config.Key;
import com.esun.cheetah.network.AbsInferiorityCommunicater;
import com.esun.util.PushUtils;

/**
 * @title CheckUpgradeCommunicater
 * @description 检查是否有新版本
 * @author liugan
 */
public class CheckUpgradeCommunicater extends AbsInferiorityCommunicater {

	private int version;
	private String channel;

	private UpgradeInfo info;
	private String sysVersion;
	private String screenreso;
	private String servial;
	private int isJiHuo;
	/**
	 * @param handler
	 */
	public CheckUpgradeCommunicater(Handler handler,Context context, int version, String channel, String sysVersion, String screenreso, String servial, int isJiHuo ) {
		super(handler);
		this.version = version;
		this.channel = channel;
		this.sysVersion = sysVersion;
		this.screenreso = screenreso;
		this.servial = servial;
		this.isJiHuo = isJiHuo;
		PushUtils.isNewUUId(context);
	}
	

	@Override
	public HashMap<String, String> comHashMap() {
		HashMap<String, String> hmJson = new HashMap<String, String>();
		hmJson.put(Key.KEY_ID, BizCode.CHECK_UPGRADE);
		hmJson.put(Key.KEY_VERSION, String.valueOf(version));
		hmJson.put(Key.KEY_CHANNEL, channel);
		hmJson.put(Key.KEY_PLATFORM, Key.KEY_ANDOIRD);
		
		if(isJiHuo == 1){
			hmJson.put(Key.KEY_ISJIHUO, String.valueOf(isJiHuo));
			hmJson.put(Key.KEY_SYS_VERSION, sysVersion);
			hmJson.put(Key.KEY_SCREENRESO, screenreso);
			//代表的push
			hmJson.put(Key.KEY_SERVIAL, PushUtils.getPushUnique());
			hmJson.put(Key.IMEI, servial);
		}
		
		return hmJson;
	}

	@Override
	public void getJsonContent(String serverRes) throws JSONException {
		JSONObject jsonObject = new JSONObject(serverRes);
		JSONObject data = jsonObject.getJSONObject(Key.KEY_DATA);
		info = new UpgradeInfo();
		if (data.getInt(Key.KEY_ISUPDATE) == 0) {
			info.isUpgrade = false;
			return;
		} else {
			info.isUpgrade = true;
			info.isMustUpgrade = (data.getInt(Key.KEY_ISMUST) == 1) ? true
					: false;
			info.upgradeDescription = data.getString(Key.KEY_VERSION_INFO);
			info.url = data.getString(Key.KEY_APK_URL);
		}

	}

	@Override
	public void getServerResponseSuccess() {
		Message msg = handlerFromAct.obtainMessage(Config.COMMUNICATER_SUCCESS);
		msg.obj = info;
		handlerFromAct.sendMessage(msg);
	}

	public static class UpgradeInfo {
		public String url;
		public boolean isUpgrade;
		public boolean isMustUpgrade;
		public String upgradeDescription;
	}

}
